package com.zjsd.sqeybusiness.service.impl;

import java.util.Date;
import java.util.List;

import com.zjsd.sqeybusiness.domain.Tcdetail;
import com.zjsd.sqeybusiness.mapper.TcdetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.TcinfoMapper;
import com.zjsd.sqeybusiness.domain.Tcinfo;
import com.zjsd.sqeybusiness.service.ITcinfoService;

/**
 * 套餐信息Service业务层处理
 *
 * @author Zheng
 * @date 2022-05-10
 */
@Service
public class TcinfoServiceImpl implements ITcinfoService
{
    @Autowired
    private TcinfoMapper tcinfoMapper;

    @Autowired
    private TcdetailMapper tcdetailMapper ;

    /**
     * 查询套餐信息
     *
     * @param tcid 套餐信息ID
     * @return 套餐信息
     */
    @Override
    public Tcinfo selectTcinfoById(Long tcid)
    {
        Tcinfo tcinfo = tcinfoMapper.selectTcinfoById(tcid);
        Tcdetail tcdetail = new Tcdetail() ;
        tcdetail.setTcid(tcinfo.getTcid());
        List<Tcdetail> tcdetailList = tcdetailMapper.selectTcdetailList(tcdetail);
        tcinfo.setTcdetail(tcdetailList);
        return tcinfo;
    }

    /**
     * 查询套餐信息列表
     *
     * @param tcinfo 套餐信息
     * @return 套餐信息
     */
    @Override
    public List<Tcinfo> selectTcinfoList(Tcinfo tcinfo)
    {
        List<Tcinfo> list = tcinfoMapper.selectTcinfoList(tcinfo) ;
        if(list.size() > 0 ){
            for(Tcinfo temp : list){
                Tcdetail tcdetail = new Tcdetail() ;
                tcdetail.setTcid(temp.getTcid());
                List<Tcdetail> tcdetailList = tcdetailMapper.selectTcdetailList(tcdetail);
                temp.setTcdetail(tcdetailList);
            }
        }
        return list;
    }

    /**
     * 新增套餐信息
     *
     * @param tcinfo 套餐信息
     * @return 结果
     */
    @Override
    public int insertTcinfo(Tcinfo tcinfo)
    {
        tcinfo.setLrtime(new Date());
        int i = tcinfoMapper.insertTcinfo(tcinfo );

        List<Tcdetail> list = tcinfo.getTcdetail() ;
        if(list != null && list.size() > 0 ){
            for (Tcdetail temp : list){
                temp.setTcid(tcinfo.getTcid());
                tcdetailMapper.insertTcdetail(temp);
            }
        }
        return i ;
    }

    /**
     * 修改套餐信息
     *
     * @param tcinfo 套餐信息
     * @return 结果
     */
    @Override
    public int updateTcinfo(Tcinfo tcinfo)
    {
        int i = tcinfoMapper.updateTcinfo(tcinfo);
        List<Tcdetail> list = tcinfo.getTcdetail() ;
        if(list.size() > 0 ){
            for(Tcdetail temp : list){
                if(temp.getDetailid() != null){
                    tcdetailMapper.updateTcdetail(temp);
                }else {
                    temp.setTcid(tcinfo.getTcid());
                    tcdetailMapper.insertTcdetail(temp);
                }
            }
        }
        return i ;
    }

    /**
     * 批量删除套餐信息
     *
     * @param tcids 需要删除的套餐信息ID
     * @return 结果
     */
    @Override
    public int deleteTcinfoByIds(Long[] tcids)
    {
        return tcinfoMapper.deleteTcinfoByIds(tcids);
    }

    /**
     * 删除套餐信息信息
     *
     * @param tcid 套餐信息ID
     * @return 结果
     */
    @Override
    public int deleteTcinfoById(Long tcid)
    {
        return tcinfoMapper.deleteTcinfoById(tcid);
    }
}
